package cn.javaxc.mvcwork.util.common;
import cn.javaxc.model.base.*;
import java.util.*;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.mvcwork.util.helper.URLHelper;
public class MapListUtil 
{ 
	


	public static List<Map> getDataValue(List<Map> _data,Object key,String kid){
		List<Map> list=new ArrayList<Map>();
		for (Map vet:_data ){
			if ((""+vet.get(kid)).equals(""+key)){
				list.add(vet);
			}
		}
		return list;
	}
	public static Map[] getDataValue(Map[] _data,String key,String kid){
		List<Map> list=new ArrayList<Map>();
		for (int i=0;i<_data.length;i++){
			Map vet=_data[i];
			if (vet.get(kid)!=null&&vet.get(kid).toString().equals(key)){
				list.add(vet);
			}
		}
		return list.toArray(new Map[0]);
	}

	public static List<Map> getNullValue(List<Map> _data,Object key,String kid){
		List<Map> list=new ArrayList<Map>();
		for (Map vet:_data ){
			if (vet.get(kid)==null){
				list.add(vet);
			}
		}
		return list;
	}
	public static Map[] getNullValue(Map[] _data,String key,String kid){
		List<Map> list=new ArrayList<Map>();
		for (int i=0;i<_data.length;i++){
			Map vet=_data[i];
			if (vet.get(kid)==null){
				list.add(vet);
			}
		}
		return list.toArray(new Map[0]);
	}

	public static Map getTopData(List<Map> _data,String key,String kid){
		for (Map vet:_data ){
			if (vet.get(kid)!=null&&vet.get(kid).equals(key)){
				return vet;
			}
		}
		return null;
	}
	public static String getDataString(List<Map> _data,String key,String kid,String vid){
		for (Map vet:_data ){
			if (vet.get(kid).equals(key)){
				return vet.get(vid)+"";
			}
		}
		return "";
	}

	public static List<Object> getDataList(SQLList<SQLMap> _data,String key){
		List list=new ArrayList();
		for (SQLMap vet:_data ){
			if (vet.getMap(key)!=null){
				list.add(vet.getMap(key));
			}
		}
		return list;
	}

	public static Set getDataSet(SQLList<SQLMap> _data,String key){
		Set list=new LinkedHashSet();
		for (SQLMap vet:_data ){
			if (vet.getMap(key)!=null){
				list.add(vet.getMap(key));
			}
		}
		return list;
	}
	public static Object[] getChartValue(SQLList<SQLMap> list,Object clmn,String clmnname,String clmnseries,String clmnvalue){
		Set<String> ss=getDataSet(list,clmnseries);
		String[] arr=ss.toArray(new String[0]);
		Object[] value=new Object[arr.length];
		List<SQLMap> sublist=getDataList(list,clmn,clmnname);
		for (int i=0;i<arr.length;i++){
			value[i]=0;
			for (SQLMap emt:sublist){
				if (arr[i].equals(emt.getMap(clmnseries))){
					value[i]=emt.getMap(clmnvalue);
					break;
				}
			}
		}
		return value;
	}
	public static List<SQLMap> getDataList(SQLList<SQLMap> _data,Object key,String kid){
		return getDataList(_data,Arrays.asList(key),kid);
	}
	public static List<SQLMap> getDataList(SQLList<SQLMap> _data,Object[] keys,String kid){
		return getDataList(_data,Arrays.asList(keys),kid);
	}
	public static List<SQLMap> getDataList(SQLList<SQLMap> _data,Collection keys,String kid){
		List<SQLMap> list=new ArrayList();
		for (SQLMap vet:_data){
			for (Object key:keys){
				if ((""+vet.getMap(kid)).equals(""+key)){
					list.add(vet);
					break;
				}
			}
		}
		return list;
	}
	public static SQLMap getTopData(SQLList<SQLMap> _data,Object key,String kid){
		for (SQLMap vet:_data ){
			if ((""+vet.getMap(kid)).equals(""+key)){
				return vet;
			}
		}
		return null;
	}

	public static SQLList buildTop(SQLList node,TreeBean tb,Object id){

		SQLList arr=new SQLList();
		SQLMap v=getTopData(node,id,tb.getTreeNodeId());
		while (v!=null){
			arr.add(v);
			if ((""+v.get(tb.getTreeNodeId())).equals(""+v.get(tb.getTreeNodePid()))){
				break;
			}
			v=getTopData(node,v.get(tb.getTreeNodePid()),tb.getTreeNodeId());
		}
		return arr;
	}

	public static SQLList buildExcelTitle(String text){
		SQLList list=new SQLList();
		SQLMap emt=new SQLMap();
		for (String s:text.split("\n")){
			String[] a=s.split("=");
			if (a.length>1){
				list.getColumnNames().add(a[1]);
				emt.put(a[1],a[0]);
			}
		}
		list.add(emt);
		return list;
	}
	public static List<SQLList> split(SQLList<SQLMap> that,String clmn){
		List<SQLList> list=new ArrayList();
		SQLList subList=null;
		String splitValue=null;
		for (SQLMap emt:that ){
			if (splitValue==null||!splitValue.equals(emt.get(clmn)+"")) {
				if (subList!=null&&subList.size()>0){
					list.add(subList);
				}
				splitValue=emt.get(clmn)+"";
				subList=new SQLList();

			}
			subList.add(emt);
		}
		if (subList!=null&&subList.size()>0){
			list.add(subList);
		}
		return list;
	}
	public static List<SQLList> split(SQLList<SQLMap> that,int clmn){
		List<SQLList> list=new ArrayList();
		SQLList subList=subList=new SQLList();
		for (int i=0;i<that.size() ;i++ ){
			subList.add(that.get(i));
			if ((i+1)%clmn==0){
				list.add(subList);
				subList=new SQLList();
			}
		}
		if (subList!=null&&subList.size()>0){
			list.add(subList);
		}
		return list;
	}
	public static List<SQLMap> hump(List<SQLMap> list){
		for (SQLMap map:list){
			for (Object k:map.keySet().toArray(new String[0])){
				map.put(StringUtils.getHumpString((String)k),map.remove(k));
			}
		}
		return list;
	}
}
